<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>修改后台用户</title>
    <link rel="stylesheet" href="plugins/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="css/comments.css" media="all">
    <link rel="stylesheet" href="css/eleTree.css">
    <style>
        .layui-form-label{
            width: 110px;
        }
        @media screen and (max-width: 450px){
            .layui-form-item .layui-input-inline {
                margin-left: 140px;
            }
        }
        input[disabled] {
            color: #A0A0A0;
            opacity: 1;
        }
        .layui-input-block{
            margin-left: 140px;
        }
        .eleTree-node.child-my-child-2{
            display: inline-block;
        }
    </style>
</head>
<body>
    <div class="maincontainer">
        <blockquote class="layui-elem-quote">
            修改后台用户
            <button type="button" class="layui-btn layui-btn-sm" style="float: right;margin-top: -4px;" onclick="window.history.back();">返回</button>
        </blockquote>
        <form class="layui-form" action="" lay-filter="example">
            <div class="layui-form-item">
                <label class="layui-form-label">账号：</label>
                <div class="layui-input-inline">
                  <input type="text" name="account" autocomplete="off" placeholder="请输入账号" class="layui-input" maxlength="20" disabled>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码：</label>
                <div class="layui-input-inline">
                    <input type="password" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input" maxlength="20">
                </div>
                <div class="layui-form-mid layui-word-aux">注：6到20位字母或数字</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">确认密码：</label>
                <div class="layui-input-inline">
                    <input type="password" name="password1" placeholder="请输入确认密码" autocomplete="off" class="layui-input" maxlength="20">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">姓名：</label>
                <div class="layui-input-inline">
                  <input type="text" name="name" autocomplete="off" placeholder="请输入姓名" class="layui-input" maxlength="20">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">性别：</label>
                <div class="layui-input-block">
                    <input type="radio" name="sex" value="1" title="男" checked="">
                    <input type="radio" name="sex" value="2"" title="女">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">角色权限：</label>
                <div class="layui-input-inline">
                    <select name="roleId" lay-filter="roleId" disabled>
                        <option value="">请选择角色</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">服务区域：</label>
                <div class="layui-input-block">
                    <div class="eleTree ele1"></div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态：</label>
                <div class="layui-input-block">
                    <input type="radio" name="openStatus" value="1" title="启用" checked="" disabled>
                    <input type="radio" name="openStatus" value="0" title="禁用" disabled>
                </div>
            </div>
            <div class="layui-form-item">
              <div class="layui-input-block">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="demo1">确认</button>
                <button type="button" class="layui-btn layui-btn-primary" onclick="window.history.back()">取消</button>
              </div>
            </div>
        </form>
    </div>
<script type="text/javascript" src="plugins/layui/layui.js"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script>

    layui.config({
        base: 'plugins/layui/modules/',
    }).use(['http', 'form', 'eleTree'], function() {
        var http = layui.http,
            form = layui.form,
            eleTree = layui.eleTree;

        var id = http.getPid('id')
        // 所属角色查询
        http.get('admin/role/listAll',{},function(res){
            if(res.code == 200){
                var html = '<option value="">请选择角色</option>';
				var data = res.data;
				for (var i = 0; i < data.length; i++) {
					html += '<option value="' + data[i].id + '">' + data[i].roleName + '</option>'
				}
				$('select[name=roleId]').html(html);
                form.render('select');
                getDetail()
            }else{
                layer.msg(res.msg)
            }
        })

        //用户权限
        var el = '';
        function getPermission(regionIdSet){
            // http.get('admin/region/listAllRegion',{},function(res){
            //     if(res.code == 200){
            //         var data = res.data;
                    el = eleTree.render({
                        elem: '.ele1',
                        url: TUrl + 'admin/region/listAllRegion',
                        method: 'get',
                        // data: data,
                        showCheckbox: true,
                        defaultExpandAll:true,
                        defaultCheckedKeys: regionIdSet,
                        request: {     // 对后台返回的数据格式重新定义
                            name: "regionName",
                            key: "id",
                            children: "children",
                            checked: "checked",
                            disabled: "disabled",
                            isLeaf: "isLeaf",
                            level: "level"
                        },
                        response: {
                            statusName: "code",
                            statusCode: 200,
                            dataName: "data"
                        },
                        done: function(res) {
                            // console.log(res)
                            $(".child-my-child-2").hover(function(){
                                var nodedata = $(this).data('nodedata')
                                if(nodedata)
                                    tip_index=layer.tips(nodedata, this, {
                                        time:0
                                    });
                            },function(){
                                layer.close(tip_index);
                            });
                        }
                    });
            //     }
            // });
        }

        // 用户详情
        function getDetail(){
            http.get('admin/admin/findById',{
                id: id
            },function(res){
                if(res.code == 200){
                    if(res.data.id != 1){
                        $('input[name=account]').attr('disabled', false)
                        $('select[name=roleId]').attr('disabled', false)
                        $('input[name=openStatus]').attr('disabled', false)
                    }
                    form.val('example', {
                        "account": res.data.account
                        ,"password": '1234'
                        ,"password1": '1234'
                        ,"name": res.data.name
                        ,"sex": res.data.sex
                        ,"roleId": res.data.roleId
                        ,"openStatus": res.data.openStatus
                    });
                    getPermission(res.data.regionIdSet);
                }else{
                    layer.msg(res.msg)
                }
            })

        }
        
        //监听提交
        form.on('submit(demo1)', function(data){
            // console.log(data.field)
            if($.trim(data.field.account) == "") {
                layer.msg('请输入账号')
                return
            }
            if($.trim(data.field.password) == "") {
                layer.msg('请输入密码')
                return
            }
            if(data.field.password != '1234'){
                if(!/^[0-9a-zA-Z]{6,20}$/.test(data.field.password)){
                    layer.msg('密码不规范')
                    return
                }
            }
            if(data.field.password != data.field.password1){
                layer.msg('密码不一致')
                return
            }
            if($.trim(data.field.name) == "") {
                layer.msg('请输入姓名')
                return
            }
            if($.trim(data.field.roleId) == "") {
                layer.msg('请选择角色')
                return
            }
            var checkedList = el.getChecked(false, false);
            var regionIdSet = [];
            for(var i=0;i<checkedList.length;i++){
                if(checkedList[i].level == 2)
                regionIdSet.push(checkedList[i].id);
            };
            if(regionIdSet.length == 0){
                layer.msg('请选择服务区域')
                return
            }

            var params = {
                id: id,
                account: $.trim(data.field.account),
                name: data.field.name,
                sex: data.field.sex,
                roleId: data.field.roleId,
                openStatus: data.field.openStatus,
                regionIdSet: regionIdSet
            }
            if(data.field.password != '1234'){
                params.password = data.field.password
            }
            http.postJSON('admin/admin/update',JSON.stringify(params),function(res){
                if(res.code == 200){
                    layer.msg('操作成功')
                    setTimeout(function(){
                        window.history.back()
                    },500)
                }else{
                    layer.msg(res.msg)
                }
            })
        });
		
		// 失焦验证密码
		$("input[name=password]").blur(function(e){
			let password1 = $('input[name=password1]').val();
			http.verifPwd($(this).val(),password1,1);
		}) 
		$("input[name=password1]").blur(function(e){
			let password = $('input[name=password]').val();
			http.verifPwd(password,$(this).val(),2);
		}) 
    })
</script>
</body>
</html>